﻿#include "framework.h"
#include "BaseGameObject.h"
#include "Const.h"

// 函数声明
static void BaseGameObject_Draw(BaseGameObject*);
static BaseGameObject* BaseGameObject_Move(BaseGameObject*, UINT, MoveResult*);

/**
 * 创建物体。
 * @param drawer    绘图者
 * @param type      类型
 * @param x, y      左上角坐标
 * @return 物体
 */
BaseGameObject* New_BaseGameObject(HDC drawer, GameObjectType type, int x, int y) {
    TCHAR imageFilePath[MAX_PATH] = { 0 };
    BaseGameObject* baseGameObject = (BaseGameObject*)calloc(1, sizeof(BaseGameObject));

    // 初始化数据
    baseGameObject->mDrawer = drawer;
    baseGameObject->mType = type;
    baseGameObject->mX = x;
    baseGameObject->mY = y;
    _stprintf_s(imageFilePath, MAX_PATH, IMAGE_FILE_PATH, type);
    baseGameObject->mImage = LoadImage(NULL, imageFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    baseGameObject->mImageDC = CreateCompatibleDC(drawer);
    SelectObject(baseGameObject->mImageDC, baseGameObject->mImage);

    // 初始化函数
    baseGameObject->deleteSelf = BaseGameObject_Delete;
    baseGameObject->draw = BaseGameObject_Draw;
    baseGameObject->move = BaseGameObject_Move;

    // 绘制自身
    baseGameObject->draw(baseGameObject);

    return baseGameObject;
}

/**
 * 删除基本游戏物体。
 * @param baseGameObject    基本游戏物体
 */
void BaseGameObject_Delete(BaseGameObject* baseGameObject) {
    DeleteObject(baseGameObject->mImage);
    DeleteDC(baseGameObject->mImageDC);
    free(baseGameObject);
}

/**
 * 绘制基本游戏物体。
 * @param baseGameObject    基本游戏物体
 */
static void BaseGameObject_Draw(BaseGameObject* baseGameObject) {
    BitBlt(baseGameObject->mDrawer, baseGameObject->mX * GRID_SIZE, baseGameObject->mY * GRID_SIZE,
        GRID_SIZE, GRID_SIZE, baseGameObject->mImageDC, 0, 0, SRCCOPY);
}

/**
 * 移动。
 * @param baseGameObject    基本游戏物体
 * @param direction         方向
 * @param moveResult        移动结果
 * @return 被推动的箱子
 */
static BaseGameObject* BaseGameObject_Move(BaseGameObject* baseGameObject, UINT direction, MoveResult* moveResult) {
    UNREFERENCED_PARAMETER(baseGameObject);
    UNREFERENCED_PARAMETER(direction);
    
    *moveResult = CANNOT_MOVE;
    return NULL;
}
